//
//  SYRetweetView.swift
//  SYWeibo
//
//  Created by Saya on 16/4/13.
//  Copyright © 2016年 黄金果. All rights reserved.
//

import UIKit
import SnapKit

class SYRetweetView: UIView {
    private var tempConstraint:Constraint?
    //数据属性
    var statusModel:SYCellStatusViewModel?{
        didSet{
            contentLb.text = statusModel?.status?.retweeted_status?.text
            
            guard let tempModel = statusModel else {
                print("该日志为纯日志")
                return
            }
            calSelfBottomConstraint(tempModel)
        }
    }
    
    private func calSelfBottomConstraint(statusModel:SYCellStatusViewModel){
        self.tempConstraint?.uninstall()
        if statusModel.status?.retweeted_status?.pic_urls?.count <= 0 {
            //该微博没有图片
            //更行约束
            self.snp_updateConstraints(closure: { (make) -> Void in
                self.tempConstraint = make.bottom.equalTo(contentLb.snp_bottom).offset(marginForCell).constraint
            })
            //设置图片view为隐藏
            pictureView.hidden = true
            //将图片view的数据设置为nil
            pictureView.pic_urls = nil
        }else{
            //该微博有图片
            //更行约束
            self.snp_updateConstraints(closure: { (make) -> Void in
                self.tempConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(marginForCell).constraint
            })
            //设置图片view为显示
            pictureView.hidden = false
            //设置数据
            pictureView.pic_urls = statusModel.status?.retweeted_status?.pic_urls
        }
    }

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
        backgroundColor = UIColor.lightGrayColor()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI (){
        addSubview(contentLb)
        addSubview(pictureView)
        
        contentLb.snp_makeConstraints { (make) -> Void in
            make.top.left.equalTo(self).offset(marginForCell)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLb.snp_bottom).offset(marginForCell)
            make.left.equalTo(self.snp_left).offset(marginForCell)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            self.tempConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(marginForCell).constraint
        }
        
    }
    
    //此时懒加载时设置的文案等是为了初始化的时候占位
    lazy var contentLb:UILabel = UILabel(text: "转发.saya大程序啊啦拉啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦拉拉拉拉啦啦啦啦啦666.saya大程序啊啦拉啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦拉拉拉拉啦啦啦啦啦", font: 14, textColor: UIColor.darkGrayColor(), margin: marginForCell)
    lazy var pictureView:SYPictureView = SYPictureView()

}
